package com.spark.tags;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;
import com.spark.util.IDGenerator;
import com.spark.util.ReferenceType;

/**
 * 默认基础组件.
 * 
 * @version $Id: Component.java v 1.0 2010-12-10 下午01:38:35 IBM Exp $
 * @author <a href="mailto:njq.niu@hand-china.com">vincent</a>
 */
public class Component extends SparkTag{
	
	private static final String DEFAULT_TEMPLATE_DIR = "templates";
	
	public static final String EXT_CORE_JAVASCRIPT = "core/ext-core-3.4.1-debug.js";
	public static final String DEFAULT_CORE_JAVASCRIPT = "core/Spark-all.js";
	public static final String DEFAULT_CORE_STYLESHEET = "core/Spark-all.css";
	
	public static final String DEFAULT_NAME_SPACE = "com.spark.ui";
	public static final String DEFAULT_TAG_PREFIX = "s";
	
	public static final String CONFIG = "config";
	
	public static final String PROPERTITY_ID = "id";
	public static final String PROPERTITY_NAME = "name";	
	public static final String PROPERTITY_VALUE = "value";
	public static final String PROPERTITY_BIND = "bind";
	public static final String PROPERTITY_BINDTARGET = "bindTarget";
	public static final String PROPERTITY_HIDDEN = "hidden";
	public static final String PROPERTITY_EVENTS = "events";
	public static final String PROPERTITY_EVENT = "event";	
	public static final String PROPERTITY_EVENT_LISTENERS = "listeners";
	public static final String PROPERTITY_EVENT_NAME = "name";
	public static final String PROPERTITY_EVENT_HANDLER = "handler";
	public static final String PROPERTITY_EVENT_SCOPE = "scope";
	public static final String PROPERTITY_EVENT_DELAY = "delay";
	
	protected String getDefaultClass(XMap view, SparkContext context){
		return "";
	}
	
	@SuppressWarnings("unchecked")
	public void init(XMap view, SparkContext context) throws Exception{
		super.init(view, context);
		context.addJavaScript(EXT_CORE_JAVASCRIPT);
		context.addJavaScript(DEFAULT_CORE_JAVASCRIPT);
		context.addStyleSheet(DEFAULT_CORE_STYLESHEET);
		
		/***************** ID属性 *****************/
		String id = view.getString(PROPERTITY_ID, "");
		if("".equals(id)) {
			id = IDGenerator.getInstance().generate();
		}
		List<String> idList = context.getIdList();
		if(idList != null && this instanceof GridLayout == false){
			idList.add(id);			
		} 
		context.put(PROPERTITY_ID, id);
		context.addJsonConfig(PROPERTITY_ID, id);
		/*****************************************/
		
		
		/***************** NAME属性 *****************/
		String name = view.getString(PROPERTITY_NAME, "");
		if(!"".equals(name)) {
			context.put(PROPERTITY_NAME, name);
			context.addJsonConfig(PROPERTITY_NAME,name);
		}
		/*****************************************/
		
		
		/***************** Value属性 *****************/
		String value = view.getString(PROPERTITY_VALUE, "");
		context.put(PROPERTITY_VALUE, value);
		if(!"".equals(value))
		context.addJsonConfig(PROPERTITY_VALUE,value);
		/*****************************************/
		
		
		/***************** 注册事件 *****************/
		XMap events = view.getChild(PROPERTITY_EVENTS);
		Map<String,HashMap> listeners = (Map<String,HashMap>)context.get(PROPERTITY_EVENT_LISTENERS);
		if(listeners==null){
			listeners = new HashMap<String,HashMap>();
			
		}
		if(events != null){
			List<XMap> list = events.getChilds();
			if(list != null){
				Iterator<XMap> it = list.iterator();
				while(it.hasNext()){
					XMap event = it.next();
					String eventName = event.getString(PROPERTITY_EVENT_NAME,"");
					String handler = event.getString(PROPERTITY_EVENT_HANDLER,"");
					String scope = event.getString(PROPERTITY_EVENT_SCOPE,"");
					Integer delay = event.getInt(PROPERTITY_EVENT_DELAY,0);
					if(!"".equals(eventName)) {
						HashMap et = new HashMap();
						if(!"".equals(handler)) et.put("fn", new ReferenceType(handler));
						if(!"".equals(scope)) et.put("scope", new ReferenceType(scope));
						if(delay!=0) et.put("delay", delay);
						listeners.put(eventName, et);
					}
				}
				context.addJsonConfig(PROPERTITY_EVENT_LISTENERS, listeners);
			}
		}
		/*****************************************/
		
		
		/***************** 绑定DataSet *****************/
		String bindTarget = view.getString(PROPERTITY_BINDTARGET, "");
		if(!bindTarget.equals("")){	
			context.addJsonConfig("bt", bindTarget);
		}
	}

	public String execute(XMap view, SparkContext context) throws Exception {
		context.put(Component.CONFIG, context.getJsonConfig());
		String template = getTemplate();
		return super.build(context, DEFAULT_TEMPLATE_DIR + "/" + template);
	}
	
	
	
	public String getTemplate(){
		return null;
	}
	
	
	protected boolean isIE6OrIE7(SparkContext context){
		String userAgent = context.getUserAgent().toUpperCase();
		return userAgent.contains("MSIE 6.0") || userAgent.contains("MSIE 7.0");
		
	}

}
